define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/user_section/index' + location.search,
                    add_url: 'course/user_section/add',
                    edit_url: 'course/user_section/edit',
                    del_url: 'course/user_section/del',
                    multi_url: 'course/user_section/multi',
                    import_url: 'course/user_section/import',
                    table: 'user_section',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('User_id')},
                        {field: 'section_name', title: __('Section_id')},
                        {field: 'progress', title: __('Progress'), operate:'BETWEEN'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'studytime', title: __('Studytime'), operate: false,formatter:function (value,row) {
                                if(value){
                                    let theTime = parseInt(value); // 秒
                                    let middle = 0; // 分
                                    if (theTime > 60) {
                                        middle = parseInt(theTime / 60);
                                        theTime = parseInt(theTime % 60);
                                    }
                                    return `${middle>9?middle:'0'+middle}′${theTime>9?theTime:'0'+theTime}″`
                                }else{
                                    return "-";
                                }
                            }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'check_status', title: __('Check_status'), searchList: {"0":__('Check_status 0'),"1":__('Check_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'study_status', title: __('Study_status'), searchList: {"0":__('Study_status 0'),"1":__('Study_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'course/user_section/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'course/user_section/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'course/user_section/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        info: function() {
            $(document).on("click",".btn-print",function () {
                let office = document.getElementsByClassName('container')[0];

                var imgs = office.getElementsByTagName('img');
                var img,src;
                if(!imgs.length){
                    src = window.getComputedStyle(office,null).getPropertyValue('background-image').replace(/(^url)|\(|\)|\"/g,'');
                    img = document.createElement('img');
                    img.src=src;
                    img.height = 150;
                    img.width = 150;
                    office.style.backgroundImage = 'none';
                    office.appendChild(img);
                }
                console.log(office);
                window.print();
            })

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});